/*
███████╗██╗   ██╗ ██████╗██╗  ██╗
██╔════╝██║   ██║██╔════╝██║ ██╔╝
█████╗  ██║   ██║██║     █████╔╝ 
██╔══╝  ██║   ██║██║     ██╔═██╗ 
██║     ╚██████╔╝╚██████╗██║  ██╗
╚═╝      ╚═════╝  ╚═════╝╚═╝  ╚═╝
*/
package common.utils;

import entity.User;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TokenManager {
    // 使用ConcurrentHashMap存储Token（线程安全）
    private static final Map<String, User> TOKEN_MAP = new ConcurrentHashMap<>();
    static {
        // 这段代码只会在 TokenManager 这个类第一次被加载到JVM时执行一次
        System.out.println("==========================================================");
        System.out.println("=== TokenManager Class Initialized! Map is now brand new. ===");
        System.out.println("==========================================================");
    }

    public static void putToken(String token, User user) {
        System.out.println("[TokenManager] Putting token: " + token);
        TOKEN_MAP.put(token, user);
    }

    public static User getUserByToken(String token) {
        System.out.println("[TokenManager] Getting user for token: " + token);
        User user = TOKEN_MAP.get(token);
        if (user == null) {
            System.out.println("[TokenManager] User NOT FOUND for this token!");
        } else {
            System.out.println("[TokenManager] User FOUND: " + user.getUsername());
        }
        return user;
    }

    public static void removeToken(String token) {
        TOKEN_MAP.remove(token);
    }
}